package com.dhcc.utils;

import java.util.Random;

public class __Random {
	private static final Random RND = new Random();
	
	public static final int DIGIT = 1;
	private static final String DIGITS = "0123456789";
	public static final int LOWER_CHAR = 2;
	private static final String LOWER_CHARS = "abcdefghijklmnopqrstuvwxyz";
	public static final int UPPER_CHAR = 4;
	private static final String UPPER_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public static final int SYMBOL = 8;
	private static final String SYMBOLS = "'!\"#$%&'()*+-,./\\:;<>=?@^[]_`~{}|'";
	
	
	/**
	 * 获取随机字符串（由数字+大写字母组成）
	 * @param length
	 * @return
	 */
	public static String nextString(int length){
		return nextString(length, DIGIT|UPPER_CHAR);
	}
	
	/**
	 * 获取客户端ip地址
	 * @param length 随机字符串长度
	 * @param options 选项 数字、小写字母、大写字母、符号
	 * @return
	 */
	public static String nextString(int length, int options){
		char[] chars = new char[length];
		StringBuilder builder = new StringBuilder(128);
		if((options&DIGIT)==DIGIT){
			builder.append(DIGITS);
		}
		if((options&LOWER_CHAR)==LOWER_CHAR){
			builder.append(LOWER_CHARS);
		}
		if((options&UPPER_CHAR)==UPPER_CHAR){
			builder.append(UPPER_CHARS);
		}
		if((options&SYMBOL)==SYMBOL){
			builder.append(SYMBOLS);
		}
		
		int pos = 0;
		int bound = builder.length();
		while(pos<length){
			int index = RND.nextInt(bound);
			chars[pos++] = builder.charAt(index);
		}
		return new String(chars);
	}
}
